/* A file that contains usefull multi lined definitions */
/* so as to achieve clearer source files */

/* WARNING: a 'int nbytes' must be declared from the calling code */

/* Writes B on A . Exit if failure */
#ifndef _DFS_MULTIES
#define _DFS_MULTIES
#define write_to_fd_on_fail_exit(A,B,C) nbytes = write(A,B,strlen(B)); \
if(nbytes == 0){ \
	if(errno != 0) \
		perror("Write did not send any data"); \
	else \
		printf("Write did not send any data to socket %d \n",A); \
} \
if(nbytes < 0){ \
	perror(C); \
	exit(errno); \
}

/* Reads from  A and store it on B . First B is cleared . Exit if failure*/	
#define read_from_fd_on_fail_exit(A,B,C) memset(B,0,sizeof(B)); \
nbytes = read(A,B,sizeof(B)); \
if(nbytes == 0){ \
	if(errno != 0) \
		perror("Socket/Pipe hanged up"); \
	else \
		printf("Socket %d disconnected\n",A); \
} \
if(nbytes < 0){ \
	perror(C); \
	exit(errno); \
}
	
/* Reads from A and store it on B . Just print failure */
#define read_from_fd(A,B,C) memset(B,0,sizeof(B)); \
nbytes = read(A,B,sizeof(B)); \
if(nbytes == 0){ \
	if(errno != 0) \
		perror("Socket/Pipe hanged up"); \
	else \
		printf("Socket %d disconnected\n",A); \
} \
if(nbytes < 0) perror(C)

/* Write B on A . Just print failure */
#define write_to_fd(A,B,C) nbytes = write(A,B,strlen(B)); \
if(nbytes == 0){ \
	if(errno != 0) \
		perror("Write did not send any data"); \
	else \
		printf("Write did not send any data to socket %d \n",A); \
} \
if(nbytes < 0){ \
	perror(C); \
}

/* error checking*/
#define check_connection(A) if(A == NOTCONNECTED){\
	printf("[THIS COMMAND REQUIRES CLIENT TO BE CONNECTED TO A SERVER]\n"); \
	continue; \
}	

#define check_connection_and_params(A,B,C) if(A == NOTCONNECTED){ \
	printf("[THIS COMMAND REQUIRES CLIENT TO BE CONNECTED TO A SERVER]\n"); \
	continue; \
} \
if(B < C){ \
	printf("[THIS COMMAND REQUIRES MORE ARGUMENTS]\n"); \
	continue; \
} 
#endif

